package ce.com.newbluedemo.k2;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import ce.com.cenewbluesdk.entity.k2.K2_SendNote;
import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.cenewbluesdk.uitl.TimeUtil;
import ce.com.newbluedemo.R;

/**
 * Created by YaoBinkun on 2016/12/29.
 */

public class ActivitySendNote extends K2BaseActivity implements RadioGroup.OnCheckedChangeListener, View.OnFocusChangeListener {
    private int id;    //信息唯一ID号
    private int enable;//0：删除，1：使能此日志，2：停止。
    private int time_type;    //0: 太阳历 ， 1：月亮历
    private int start_time;//日志提醒时间，精确到秒值，为绝对时间。
    private int end_time;//结束时间，在此范围内有效
    private int time_num;//提前时间：[0~7]: 0:0min , 1: 5min , 2:10min,3:30min, 4:1hour, 5:1day, 6: 3days.
    private int repeatType;//重复类型：0、不重复，仅单次，1、周重复，2、日重复，3、月重复，4、年重复。
    private int repeatday;//周重复的天数，[0~6]每一位代码相应的周几，0:周日，1:周一，……，6:周六。当重复类型为其它时则此值为0.
    private int len;//实际数据长度值。
    private String context;//日志主题内容，为字符串型，其长度最大不超过40字节。

    private int noteType;

    private byte[] reserved = new byte[3];

    private RadioGroup mRG1, mRG2, mRG3, mRG4, mRG5, mRG6;
    private CEDevK2Proxy k2Proxy;

    private EditText mEtContent, mEtStartTime, mEtStopTime, mEtNoteId;

    private Handler mK2Handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0) {
                showToast("发送失败，请稍后重试！");
            } else {
                mEtContent.setText("");
                mEtStopTime.setText("");
                mEtStartTime.setText("");
                mEtNoteId.setText("");
                mRG1.clearCheck();
                mRG2.clearCheck();
                mRG3.clearCheck();
                mRG4.clearCheck();
                mRG5.clearCheck();
                mRG6.clearCheck();
                showToast("发送成功！");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_note);
        k2Proxy = BleFactory.getInstance().getK2Proxy();
        k2Proxy.setHandler(mK2Handler);
        mRG1 = (RadioGroup) findViewById(R.id.rg_note_enable);
        mRG2 = (RadioGroup) findViewById(R.id.rg_time_type);
        mRG3 = (RadioGroup) findViewById(R.id.rg_time_ahead);
        mRG4 = (RadioGroup) findViewById(R.id.rg_repeat_type);
        mRG5 = (RadioGroup) findViewById(R.id.rg_repeat_day);
        mRG6 = (RadioGroup) findViewById(R.id.rg_note_type);
        mRG6.setOnCheckedChangeListener(this);
        mRG1.setOnCheckedChangeListener(this);
        mRG2.setOnCheckedChangeListener(this);
        mRG3.setOnCheckedChangeListener(this);
        mRG4.setOnCheckedChangeListener(this);
        mRG5.setOnCheckedChangeListener(this);
        mEtContent = (EditText) findViewById(R.id.et_note_content);
        mEtContent.setOnFocusChangeListener(this);
        mEtStartTime = (EditText) findViewById(R.id.et_start_time);
        mEtStartTime.setOnFocusChangeListener(this);
        mEtStopTime = (EditText) findViewById(R.id.et_end_time);
        mEtStopTime.setOnFocusChangeListener(this);
        mEtNoteId = (EditText) findViewById(R.id.et_note_id);
        mEtNoteId.setOnFocusChangeListener(this);
        findViewById(R.id.btn_send_note).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!k2Proxy.isConnectOK()) {
                    showToast("蓝牙已经断开");
                    return;
                }
                if (mRG1.getCheckedRadioButtonId() == -1 ||
                        mRG2.getCheckedRadioButtonId() == -1 ||
                        mRG3.getCheckedRadioButtonId() == -1 ||
                        mRG4.getCheckedRadioButtonId() == -1 ||
                        mRG5.getCheckedRadioButtonId() == -1 ||
                        mRG6.getCheckedRadioButtonId() == -1) {
                    showToast("请检查输入");
                    return;
                }

                K2_SendNote k2_sendNote = new K2_SendNote(id, enable, time_type, start_time, end_time
                        , repeatType, noteType, context);
                k2_sendNote.setNoteType(1);
                k2Proxy.getSendHelper().send_Note(k2_sendNote);
            }
        });


    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.rg_note_enable:
                switch (checkedId) {
                    case R.id.rb_delete:
                        enable = 0;
                        break;
                    case R.id.rb_enable:
                        enable = 1;
                        break;
                    case R.id.rb_stop:
                        enable = 2;
                        break;
                    default:
                        break;
                }
                break;
            case R.id.rg_time_type:
                switch (checkedId) {
                    case R.id.rb_sun:
                        time_type = 0;
                        break;
                    case R.id.rb_moon:
                        time_type = 1;
                        break;
                    default:
                        break;
                }
                break;
            case R.id.rg_time_ahead:
                switch (checkedId) {
                    case R.id.rb_0:
                        time_num = 0;
                        break;
                    case R.id.rb_5:
                        time_num = 1;
                        break;
                    case R.id.rb_10:
                        time_num = 2;
                        break;
                    case R.id.rb_30:
                        time_num = 3;
                        break;
                    case R.id.rb_1hour:
                        time_num = 4;
                        break;
                    case R.id.rb_1day:
                        time_num = 5;
                        break;
                    case R.id.rb_3days:
                        time_num = 6;
                        break;
                    default:
                        break;
                }
                break;
            case R.id.rg_repeat_type:
                switch (checkedId) {
                    case R.id.rb_no_repeat:
                        repeatType = 0;
                        break;
                    case R.id.rb_repeat_weekly:
                        repeatType = 1;
                        break;
                    case R.id.rb_repeat_daily:
                        repeatType = 2;
                        break;
                    case R.id.rb_repeat_monthly:
                        repeatType = 3;
                        break;
                    case R.id.rb_repeat_yearly:
                        repeatType = 4;
                        break;
                    default:
                        break;
                }
                break;
            case R.id.rg_repeat_day:
                switch (checkedId) {
                    case R.id.rb_sunday:
                        repeatday = 0;
                        break;
                    case R.id.rb_mon:
                        repeatday = 1;
                        break;
                    case R.id.rb_tuesday:
                        repeatday = 2;
                        break;
                    case R.id.rb_wednesday:
                        repeatday = 3;
                        break;
                    case R.id.rb_thursday:
                        repeatday = 4;
                        break;
                    case R.id.rb_fri:
                        repeatday = 5;
                        break;
                    case R.id.rb_sat:
                        repeatday = 6;
                        break;
                    default:
                        break;
                }
                break;
            case R.id.rg_note_type:
                switch (checkedId){
                    case R.id.rb_meeting:
                        noteType = 0;
                        break;
                    case R.id.rb_item:
                        noteType = 1;
                        break;
                    case R.id.rb_memorial_day:
                        noteType = 2;
                        break;
                    default:
                        break;
                }
            default:
                break;
        }
    }

    private void showToast(String input) {
        Toast.makeText(this, input, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) return;
        switch (v.getId()) {
            case R.id.et_note_content:
                String inputContent = mEtContent.getText().toString();
                if (inputContent.getBytes().length > 40) {
                    showToast("超过最大长度，请重试");
                    return;
                }
                context = inputContent;
                break;
            case R.id.et_start_time:
                String inputStartTime = mEtContent.getText().toString();
                String[] startTime = inputStartTime.split(":");
                StringBuffer date = new StringBuffer();
                date = date.append(startTime[0]).append("-" + startTime[1]).append("-" + startTime[2]);
                StringBuffer time = new StringBuffer();
                time = time.append(startTime[3]).append(":" + startTime[4]).append(":" + startTime[5]).append(".000");
                start_time = (int) string2millionseconds(date + " " + time) / 1000;
                break;
            case R.id.et_end_time:
                String inputEndTime = mEtContent.getText().toString();
                String[] endTime = inputEndTime.split(":");
                StringBuffer enddate = new StringBuffer();
                enddate = enddate.append(endTime[0]).append("-" + endTime[1]).append("-" + endTime[2]);
                StringBuffer endtime = new StringBuffer();
                endtime = endtime.append(endTime[3]).append(":" + endTime[4]).append(":" + endTime[5]).append(".000");
                end_time = (int) string2millionseconds(enddate + " " + endtime) / 1000;
                break;
            case R.id.et_note_id:
                String input = mEtNoteId.getText().toString();
                if (TextUtils.isEmpty(input) || !LittleUtils.isInteger(input)) {
                    showToast("id输入有误");
                    return;
                }
                id = Integer.parseInt(input);
                break;
            default:
                break;
        }
    }

    public long string2millionseconds(String input) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            return sdf.parse(input).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }


}
